package org.weso.lodin.utils;


import java.io.File;
import java.io.IOException;
import java.util.Map;
import java.util.ResourceBundle;

import org.weso.lodin.appserv.TransformerAppServ;
import org.weso.lodin.datasource.config.DatasetListing;
import org.weso.lodin.generated.Dataset;
import org.weso.lodin.to.CompleteModelTO;

import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.ModelFactory;


public class IndicatorsAppServMain {
	public static final String DIR_GENERATED = "generated";

	public static void main(String []args) throws IOException{
		String dir =  DatasetListing.defaultPathData;
		//String dir = "src/test/resources/xml";
		TransformerAppServ transformer;
		String outputFile = "default-observations.ttl";
		String slicesFile = "default-observations-slices.ttl";
		boolean allIn = Boolean.TRUE;
		boolean isPrimary = Boolean.FALSE;
		if(isPrimary){
			outputFile = "primary-observations.ttl";
			slicesFile = "primary-observations-slices.ttl";
			transformer = TransformerHelper.getPrimaryTransformer(dir);
		}else{
			outputFile = "secondary-observations.ttl";
			slicesFile = "secondary-observations-slices.ttl";
			transformer = TransformerHelper.getSecondaryTransformer(dir);
		}
		
		ResourceBundle mapPrefixesURIs = PrefixManager.getResourceBundle();		
		Map<Dataset, CompleteModelTO> datasetsModels = transformer.transform(isPrimary);
		if(allIn){
			Model observations = ModelFactory.createDefaultModel();
			Model slices = ModelFactory.createDefaultModel();
			for(CompleteModelTO m:datasetsModels.values()){
				observations.add(m.getObservations());
				slices.add(m.getSlices());
			}			
			TransformerHelper.serializeModel(observations,mapPrefixesURIs,DIR_GENERATED+File.separator+outputFile);
			TransformerHelper.serializeModel(slices,mapPrefixesURIs,DIR_GENERATED+File.separator+slicesFile);			
		}else{
			for(Dataset dataset:datasetsModels.keySet()){
				TransformerHelper.serializeModel(datasetsModels.get(dataset).getObservations(),mapPrefixesURIs, 
						DIR_GENERATED+File.separator+dataset.getId()+"-obs.ttl");	
				TransformerHelper.serializeModel(datasetsModels.get(dataset).getSlices(),mapPrefixesURIs, 
						DIR_GENERATED+File.separator+dataset.getId()+".ttl");	
			}		
			
		}
		
	}
	
}

